package com.wyx.videoplay;

import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.wyx.videoplay.fragment.CustomFragment;
import com.wyx.videoplay.fragment.DeviceFragment;
import com.wyx.videoplay.fragment.VideoFragment;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class MainActivity extends AppCompatActivity {

    BottomNavigationView mBnvTab;
    ViewPager mVpContainer;

    private List<Fragment> mFragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBnvTab=findViewById(R.id.layout_bottom_act_main);
        mVpContainer=findViewById(R.id.viewpager_act_main);

        mFragmentList = new ArrayList<>();
        mFragmentList.add(CustomFragment.newInstance());
        mFragmentList.add(VideoFragment.newInstance());
        mFragmentList.add(DeviceFragment.newInstance());

        FragmentPagerAdapter pagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentList.size();
            }
        };
        mVpContainer.setAdapter(pagerAdapter);
        mVpContainer.setOffscreenPageLimit(3);

        mBnvTab.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.tab_search:
                        mVpContainer.setCurrentItem(0);
                        return true;
                    case R.id.tab_manage:
                        mVpContainer.setCurrentItem(1);
                        return true;
                    case R.id.tab_mine:
                        mVpContainer.setCurrentItem(2);
                        return true;
                }
                return false;
            }
        });

        mVpContainer.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        mBnvTab.setSelectedItemId(R.id.tab_search);
                        break;
                    case 1:
                        mBnvTab.setSelectedItemId(R.id.tab_manage);
                        break;
                    case 2:
                        mBnvTab.setSelectedItemId(R.id.tab_mine);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}
